<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 商家自动接单
 */

namespace app\api\controller\common\v1_0;

class Store extends Common
{

    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 自动接单
     */    
    public function index(){
        // $store = (new \app\common\model\Store)->where(['automatic_receiving_order' => 1])->select();
        // $order  = (new \app\common\model\Orders())->alias('order')
        //           ->join("{$this->tdb}store store","store.id = order.store_id")
        //           ->field(['order.id','store.client_id'])
        //           ->where(['store.automatic_receiving_order' => 1 ,'order.state' => 2])->select();
       
        // foreach ($order as $key => $value) {
        //    (new \app\common\model\Orders())->where(['id' => $value['id']])->update([
        //     'state' => 3,'store_receiving_time' => time(),
        //    ]);
        //    (new \app\common\tui\Tui())->filter($value['client_id']);
        // }
        // return self::successResult("成功");
        $list  = (new \app\common\model\Orders())->alias('order')
                  ->join("{$this->tdb}store store","store.id = order.store_id")
                  ->field([
                    'order.id',
                    'store.client_id',
                    'order.store_id',
                    'order.is_self_mention',
                    'order.estimate_arrive_time',
                    'order.distribution_time',
                    'order.meal_time',
                    'order.pay_time',
                    ])
                  ->where(['store.automatic_receiving_order' => 1 ,'order.state' => 2])->select();
        // $list = (new \app\common\model\Orders())->where(['state' => 2])->select();
      
        foreach ($list as $key => $value) {
            #商家是否开启自动接单
            $store_automatic_receiving_order =  (new \app\common\model\Store())->where("id",$value['store_id'])->value('automatic_receiving_order');
            if($store_automatic_receiving_order){
                  #计算预计出餐时间 预计送达时间 - (配送时间(分钟) - 出餐时间(分钟) * 60)
                  $meal_time = 0;
                  if(!$value['is_self_mention']){
                    //   $meal_time = $value['estimate_arrive_time'] - (($value['distribution_time'] - $value['meal_time']) * 60);
                      $meal_time = $value['pay_time']+($value['meal_time']*60);
                      $row = (new \app\common\model\Orders())->where(['id'=>$value['id']])->update([
                                'store_state'=>'2',
                                'state'=>2,
                                "member_read_status"=>0,
                                "estimate_meal_time" => $meal_time,
                                'store_receiving_time' => time()
                     ]);
                     if($row){
                        (new \app\common\model\Orders())->store_wws($value['id']);
                        (new \app\common\model\Orders())->rider_wws($value['id']);
                        (new \app\common\model\Orders())->user_wws($value['id']);
                     }
                  }
            }
        }

    }
     /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 自动出餐
     */    
    public function dining(){
        $order  = (new \app\common\model\Orders())->alias('order')
        ->join("{$this->tdb}store store","store.id = order.store_id")
        ->field(['order.id','store.client_id','store.eat_out_time'])
        ->where(['store.is_eat_out_time' => 1 ,'order.state' => 3])
        ->select();
        foreach ($order as $key => $value) {
            // if($order['store_receiving_time']+($order['eat_out_time']*60) >= time()){
                (new \app\common\model\Orders())->where(['id' => $value['id']])->update([
                    'store_state' => 1,
                ]);
            // }
        }
        return self::successResult("成功");
    }
    
}   
